1
|
|
|
/** |
2
|
|
|
* @license |
3
|
|
|
* Copyright (c) 2020 UMI |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
6
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
7
|
|
|
* in the Software without restriction, including without limitation the rights |
8
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
10
|
|
|
* furnished to do so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included in all |
13
|
|
|
* copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
21
|
|
|
* SOFTWARE. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
'use strict' |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Декодирует массив байтов UTF-8 в строку в кодировке UTF-16. |
28
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder |
29
|
|
|
* @param {number[]} bytes Массив байтов UTF-8. |
30
|
|
|
* @returns {string} |
31
|
|
|
*/ |
32
|
|
|
function textDecode (bytes) { |
33
|
1 |
|
let str = '' |
34
|
1 |
|
let i = 0 |
35
|
1 |
|
while (i < bytes.length) { |
36
|
12 |
|
if (bytes[i] < 0x80) { |
37
|
5 |
|
str += String.fromCharCode(bytes[i++]) |
38
|
7 |
|
} else if ((bytes[i] > 0xBF) && (bytes[i] < 0xE0)) { |
39
|
2 |
|
str += String.fromCharCode((bytes[i++] & 0x1F) << 6 | bytes[i++] & 0x3F) |
40
|
5 |
|
} else if (bytes[i] > 0xDF && bytes[i] < 0xF0) { |
41
|
2 |
|
str += String.fromCharCode(((bytes[i++] & 0x0F) << 12) | ((bytes[i++] & 0x3F) << 6) | (bytes[i++] & 0x3F)) |
42
|
|
|
} else { |
43
|
3 |
|
const code = (((bytes[i++] & 0x07) << 18) | ((bytes[i++] & 0x3F) << 12) | ((bytes[i++] & 0x3F) << 6) | (bytes[i++] & 0x3F)) - 0x010000 |
44
|
3 |
|
str += String.fromCharCode(code >> 10 | 0xD800, code & 0x03FF | 0xDC00) |
45
|
|
|
} |
46
|
|
|
} |
47
|
1 |
|
return str |
48
|
|
|
} |
49
|
|
|
/** |
50
|
|
|
* Кодирует UTF-16 строку в массив байтов UTF-8. |
51
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder |
52
|
|
|
* @param {string} text Текстовая строка в кодировке UTF-16. |
53
|
|
|
* @returns {number[]} |
54
|
|
|
*/ |
55
|
|
|
function textEncode (text) { |
56
|
2 |
|
const b = [] |
57
|
2 |
|
let i = 0 |
58
|
2 |
|
while (i < text.length) { |
59
|
48 |
|
const code = text.charCodeAt(i++) |
60
|
48 |
|
if (code < 0x80) { |
61
|
41 |
|
b[b.length] = code |
62
|
7 |
|
} else if (code < 0x800) { |
63
|
2 |
|
b[b.length] = 0xc0 | (code >> 6) |
64
|
2 |
|
b[b.length] = 0x80 | (code & 0x3f) |
65
|
5 |
|
} else if (code < 0xd800 || code >= 0xe000) { |
66
|
2 |
|
b[b.length] = 0xe0 | (code >> 12) |
67
|
2 |
|
b[b.length] = 0x80 | ((code >> 6) & 0x3f) |
68
|
2 |
|
b[b.length] = 0x80 | (code & 0x3f) |
69
|
|
|
} else { |
70
|
3 |
|
encodeUtf8Mb4(b, 0x10000 + ((code & 0x3ff) << 10) + (text.charCodeAt(i++) & 0x3ff)) |
71
|
|
|
} |
72
|
|
|
} |
73
|
2 |
|
return b |
74
|
|
|
} |
75
|
|
|
/** |
76
|
|
|
* @param {number[]} b |
77
|
|
|
* @param {number} code |
78
|
|
|
* @private |
79
|
|
|
*/ |
80
|
|
|
function encodeUtf8Mb4 (b, code) { |
81
|
3 |
|
b[b.length] = 0xf0 | (code >> 18) |
82
|
3 |
|
b[b.length] = 0x80 | ((code >> 12) & 0x3f) |
83
|
3 |
|
b[b.length] = 0x80 | ((code >> 6) & 0x3f) |
84
|
3 |
|
b[b.length] = 0x80 | (code & 0x3f) |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
exports.textDecode = textDecode |
88
|
|
|
exports.textEncode = textEncode |
89
|
|
|
|